ys = [];
// 设置标签栏的随机图标颜色
for (let index = 0; index < 30; index++) {
  ys.push('data:image/svg+xml;base64,' + window.btoa(`<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <rect width="700" height="700" style="fill:#${index % 2 == 1 ? genGuid() : 'ffffff'};"/> </svg>`));
}

let myTime = document.getElementById("myTime");
let myTime2 = document.getElementById("myTime2");

let interval = null;
function openFn() {
  interval && clearInterval(interval);
  right.innerHTML = "00:00";
  let stamp_mm = (myTime.value.includes('.') || myTime.value.includes('。')) && Number(myTime.value.substr(1)) || Number(myTime.value) * 60;
  interval = setInterval(() => {
    let ff = Math.floor(stamp_mm / 60)
    let mm = stamp_mm % 60;
    left.innerHTML = (ff > 9 && ff || '0' + ff) + ':' + ((mm > 9 && mm || '0' + mm));
    window.document.title = ff + '分' + mm + "秒";
    if (stamp_mm <= 0) { // 执行内容
      interval && clearInterval(interval); interval = null;
      bgMusicFn(3); bgMusicFn(1);
      ybjc();
      setTimeout(() => {
        bgMusicFn(5); bgMusicFn(2);
      }, 30000);
      done();
      setTitleColor(true);
      xisj();
    }
    stamp_mm--;
  }, 1000);
}

let dd = null
function done() {
  let ss = 0, ff = (myTime2.value.includes('.') || myTime2.value.includes('。')) && Number(myTime2.value.substr(1)) || Number(myTime2.value) * 60;
  dd && clearInterval(dd); dd = null;
  dd = setInterval(() => {
    if (ss >= ff) { // 执行内容
      dd && clearInterval(dd); dd = null;
      bgMusicFn(4); bgMusicFn(1);
      setTimeout(() => {
        bgMusicFn(5); bgMusicFn(2);
      }, 45000)
      openFn(); xisj(1);
      setTitleColor(false);
    }
    ss++;
  }, 1000);
}

// 标题-休息时间
let sd = null;  // 闪动
function xisj(close) {
  if (close) { sd && clearInterval(sd); sd = null; return; }
  sd && clearInterval(sd); sd = null;
  let flag = false, stamp_mm = 0;
  sd = setInterval(() => {
    let ff = Math.floor(stamp_mm / 60), mm = stamp_mm % 60;
    if (flag) (window.document.title = "休息时间");
    if (!flag) {
      right.innerHTML = (ff > 9 && ff || '0' + ff) + ':' + ((mm > 9 && mm || '0' + mm));
      window.document.title = ff + '分' + mm + "秒";
    }
    stamp_mm += 0.5; flag = !flag;
  }, 500);
}

function start() {
  sd && clearInterval(sd); sd = null;
  dd && clearInterval(dd); dd = null;
  bgMusicFn(5); bgMusicFn(2);
  openFn();
}
start();

// 设置标签页的颜色。颜色提示。
let time_ = null, setTitleColor_num = 0;
function setTitleColor(flag) {
  time_ && clearInterval(time_); time_ = null;
  flag && (time_ = setInterval(() => {
    myicon.href = ys[setTitleColor_num++ % ys.length]
  }, 500))
}

bgMusic.addEventListener('pause', (event) => {
  isPauseTo.innerHTML = '音乐已经暂停'
});
bgMusic.addEventListener('play', (event) => {
  isPauseTo.innerHTML = '播放音乐中……'
});

/** 
 * 1：播放
 * 2：暂停
 * 3：换音乐
 * 4：换音乐
 * 5：重置时间
 * @author weijun.wu */
function bgMusicFn(type) {
  if (type == 1) {
    MusicSetFn() == '1' || bgMusic.play();
  } else if (type == 2) {
    MusicSetFn() == '3' || bgMusic.pause();
  } else if (type == 3) {
    bgMusic.src = "../../assets/躺在凌晨五点半的海岸.m4a"
  } else if (type == 4) {
    bgMusic.src = "../../assets/1.ogg"
  } else if (type == 5) {
    MusicSetFn() != '3' && (bgMusic.currentTime = 0)
  }
}

// 设置音乐的播放时长
function MusicSetFn(index) {
  index = index || localStorage.getItem('MusicSet') || '2';
  if (index == '1') MusicSet1.checked = true
  if (index == '2') MusicSet2.checked = true
  if (index == '3') MusicSet3.checked = true
  localStorage.setItem('MusicSet', index)
  return index || '2';
}
MusicSetFn();

// 点击播放或关闭提示音乐
function pauseTo() {
  if (bgMusic.paused) {
    bgMusicFn(1)
  } else {
    bgMusicFn(2);
  }
}

// 眼保健操
let cycles = 0
function ybjc() {
  let tt = MusicSetFn();
  if (!myCheckbox('yb')) return;
  setTimeout(() => {
    bgMusicFn(5)
    bgMusicFn(2)
    // 缓解眼部疲劳的几种方法：https://www.bilibili.com/video/BV1vQ4y1z7EW?spm_id_from=333.337.search-card.all.click
    opemNewWindow(`file:///D:\健操/眼保健操${cycles++ % 3}.mp4`, false)
  }, 1000 * ((tt == '1' && 0) || (tt == '2' && 30) || (tt == '3' && 180)));
}

// 控制眼保健操和体操的开关以及返回它们的开关类型。
function myCheckbox(_this) {
  if (!_this) return;
  if (typeof _this == 'object') {
    localStorage.setItem(_this.name, _this.checked ? '1' : '0')
  } else {
    let index = Number(localStorage.getItem(_this) || 0);
    if (index) document.getElementById(_this).checked = true
    else document.getElementById(_this).checked = false
    return index
  }
}
myCheckbox('tc');
myCheckbox('yb');
myCheckbox('tg');

// 取消所有选项，但是刷新页面后恢复之前的设置。
function clearChoose(than) {
  if (than.innerHTML.trim() != '全选') {
    myCheckbox('tc') && (document.getElementById("tc").click());
    myCheckbox('yb') && (document.getElementById("yb").click());
    myCheckbox('tg') && (document.getElementById("tg").click());
    MusicSetFn('1');
    than.innerHTML = '全选';
  } else {
    than.innerHTML = '取消';
    myCheckbox('tc') || (document.getElementById("tc").click());
    myCheckbox('yb') || (document.getElementById("yb").click());
    myCheckbox('tg') || (document.getElementById("tg").click());
    MusicSetFn('2');
  }
}

window.apIsPaused = true;  // 用来判断音乐播放器是否暂停中
/** 打开新子窗口来播放视频
 * url：要播放的视频地址 */
function opemNewWindow(videoUrl, isNewOpenWindow = true) {
  window.ap && !window.ap.paused && (apIsPaused = false)
  window.ap && window.ap.pause();
  isNewOpenWindow ? window.open("./video.html?video=" + videoUrl, 'newwindow' + genGuid(), 'height=3000, width=3000, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no, alwaysRaised=yes, z-look=yes') : window.open(videoUrl, 'newwindow2' + genGuid())
}
